//
//  DMAddressListVC.m
//  DistributionMall
//
//  Created by JHnoplan on 2018/10/17.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMAddressListVC.h"
#import "DMAddressListCell.h"
#import "DMAddAddressVC.h"

//request
#import "KMineRequest.h"
@interface DMAddressListVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *bottomBt;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation DMAddressListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self startInit];
}

-(void)startInit {
    self.navigationItem.title = @"收货地址";
    _dataArray = [NSMutableArray new];

    _tableView.delegate = self;
    _tableView.dataSource = self;
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"DMAddressListCell" bundle:nil] forCellReuseIdentifier:@"DMAddressListCell"];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getData];

}

-(void)getData{
    KMineRequest * request = [[KMineRequest alloc]initMyAddressList];
    
    [JLProgressHUDManager showLoadingHud];
    MJWeakSelf
    [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
        
        [JLProgressHUDManager hideHud];
        KMineAddressListModel * listModel = [KMineAddressListModel modelWithJSON:baseModel.data];
        
        weakSelf.dataArray = [NSMutableArray arrayWithArray:listModel.addrViews];
        [weakSelf.tableView reloadData];
        
    } failure:^(KBaseModel *errorModel) {
        [JLProgressHUDManager showErrorHud:errorModel.msg];
    }];
    
}


//添加收货地址
- (IBAction)addClick:(UIButton *)sender {
    DMAddAddressVC *vc = [DMAddAddressVC new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100;
    return self.tableView.rowHeight;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == _dataArray.count-1) {
        return 50;
    }
    return 0.01;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DMAddressListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DMAddressListCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"DMAddressListCell" owner:self options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    KMineAddressModel * model = self.dataArray[indexPath.section];
    if ([model.isDefault isEqualToString:@"Y"]) {
        cell.defaultLb.hidden = NO;
    }else {
        cell.defaultLb.hidden = YES;
    }
    
    cell.nameLb.text = model.person;
    cell.addressLb.text = [NSString stringWithFormat:@"%@%@%@%@",model.prName,model.ciName,model.arName,model.addr];
    
    MJWeakSelf
    [cell setEditBlock:^{
        DMAddAddressVC *vc = [DMAddAddressVC new];
        vc.model = model;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (self.selectedBlock) {
        KMineAddressModel * model = self.dataArray[indexPath.section]; self.selectedBlock(model);
        [self.navigationController popViewControllerAnimated:YES];
    }
}
//左滑删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
// 进入编辑模式，按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        KMineAddressModel * model = self.dataArray[indexPath.section];
        KMineRequest * request = [[KMineRequest alloc]initDeleteAddressWithKey:model.id];
        [JLProgressHUDManager showLoadingHud];
        MJWeakSelf
        [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
            [JLProgressHUDManager showSuccessHud:@"删除成功"];
            [weakSelf.dataArray removeObject:model];
            [weakSelf.tableView reloadData];
        } failure:^(KBaseModel *errorModel) {
            [JLProgressHUDManager showErrorHud:errorModel.msg];
            
        }];
        
    }
}
// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}


@end
